/*
 * Copyright (c) 2025 EasyAdmin
 * All rights reserved.
 *
 * This file is part of the [EasAdmin] project.
 * Unauthorized copying of this file, via any medium, is strictly prohibited.
 * Proprietary and confidential.
 *
 * Author: EasyAdmin ( 1073602@qq.com )
 * Website: https://www.xhxiao.com
 */
package com.easy.module.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.framework.cache.model.Dict;
import com.easy.framework.cache.utils.DictUtil;
import com.easy.framework.common.enums.CommonStatus;
import com.easy.framework.common.utils.ConvertUtil;
import com.easy.framework.common.utils.Func;
import com.easy.framework.database.utils.QueryUtil;
import com.easy.module.system.entity.SysDictData;
import com.easy.module.system.mapper.SysDictDataMapper;
import com.easy.module.system.query.SysDictDataQuery;
import com.easy.module.system.service.ISysDictDataService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 字典数据表 Service业务
 *
 * @author EasyAdmin ( 1073602@qq.com )
 * @date 2025-08-19
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService {
    /**
     * 获取查询对象
     *
     * @param query
     * @return
     */
    @Override
    public LambdaQueryWrapper<SysDictData> lambdaQuery(SysDictDataQuery query) {
        return QueryUtil.lambdaQueryOptional(query, SysDictData.class).orElse(lambdaQueryWrapper()).orderByAsc(SysDictData::getSort);
    }

    /**
     * 新增字典数据
     *
     * @param dto 字典数据
     * @return 结果
     */
    @Override
    public boolean add(SysDictData dto) {
        boolean sign = save(dto);
        if (sign) {
            setDictionaryCache(dto.getDictCode());
        }
        return sign;
    }

    /**
     * 修改字典数据
     *
     * @param dto 字典数据
     * @return 结果
     */
    @Override
    public boolean update(SysDictData dto) {
        SysDictData entity = ConvertUtil.convert(dto, SysDictData.class);
        boolean sign = updateById(entity);
        if (sign) {
            setDictionaryCache(dto.getDictCode());
        }
        return sign;
    }

    /**
     * 批量删除字典数据
     *
     * @param ids 需要删除的字典数据主键
     * @return 结果
     */
    @Override
    public boolean deleteByIds(Long[] ids) {
        for (Long id : ids) {
            SysDictData data = getById(id);
            removeById(id);
            setDictionaryCache(data.getDictCode());
        }
        return true;
    }

    @Override
    public List<SysDictData> listByCode(String dictCode) {
        return ConvertUtil.convert(list(Wrappers.<SysDictData>lambdaQuery()
                .eq(SysDictData::getStatus, CommonStatus.enabled.getValue().toString())
                .eq(SysDictData::getDictCode, dictCode).orderByAsc(SysDictData::getSort)), SysDictData.class);
    }

    @Override
    public void setDictionaryCache(String code,List<SysDictData> list) {
        List<Dict> dictList = list.stream().map(p -> new Dict(p.getId(), p.getSort(), p.getDictLabel(), p.getDictValue(), p.getDictCode(), p.getTagType(), p.getTagClass(),p.getIcon(),p.getTip(), "1".equals(p.getDictDefault()))).collect(Collectors.toList());
        DictUtil.setDictCache(code, dictList);
    }

    @Override
    public void setDictionaryCache(String code) {
        List<SysDictData> dictDataList = listByCode(code);
        setDictionaryCache(code, dictDataList);
    }

    @Override
    public List<Dict> getDictionaryCache(String code) {
        List<Dict> dictList = DictUtil.getDictCache(code);
        if (Func.isEmpty(dictList)) {
            List<SysDictData> dictDataList = listByCode(code);
            dictList = dictDataList.stream().map(p -> new Dict(p.getId(), p.getSort(), p.getDictLabel(), p.getDictValue(), p.getDictCode(), p.getTagType(), p.getTagClass(),p.getIcon(),p.getTip(), "1".equals(p.getDictDefault()))).collect(Collectors.toList());
            DictUtil.setDictCache(code, dictList);
        }
        return dictList;
    }

    @Override
    public boolean updateDictCode(String oldDictCode, String newDictCode) {
        SysDictData dictData = new SysDictData();
        dictData.setDictCode(newDictCode);
        return update(dictData, Wrappers.<SysDictData>lambdaQuery().eq(SysDictData::getDictCode, oldDictCode));
    }

    @Override
    public long countByDictCode(String dictCode) {
        return count(Wrappers.<SysDictData>lambdaQuery().eq(SysDictData::getDictCode, dictCode));
    }

}
